package com.kaiwukj.android.ufamily.mvp.model;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.GsonUtils;
import com.kaiwukj.android.mcas.integration.IRepositoryManager;
import com.kaiwukj.android.mcas.mvp.BaseModel;
import com.kaiwukj.android.ufamily.mvp.http.api.service.AccountService;
import com.kaiwukj.android.ufamily.mvp.http.api.service.CommonService;
import com.kaiwukj.android.ufamily.mvp.http.api.service.DynamicService;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.CommentParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.CommentReplyParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.DynamicListParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.DynamicPubParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.UserInfoParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.AboutGoodsResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.CommentResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DynamicReportTypeResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DynamicResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.FansResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ShieldResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.TopicResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.UserResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.DynamicContract;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;

public class DynamicModel extends BaseModel implements DynamicContract.Model {

    @Inject
    public DynamicModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    //查询主页信息
    @Override
    public Observable<UserResult> fetchPersonIndexInfo(int userId) {
        return mRepositoryManager.obtainRetrofitService(AccountService.class)
                .getUserById(userId)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<DynamicResult>> fetchDynamic(DynamicListParams params) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .fetchDynamic(params.getStartId(),
                        params.getPageSize(),
                        params.getTopicId(),
                        params.getTopicTitle(),
                        params.getIsHot(),
                        params.getType(),
                        params.getUserId())
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<DynamicResult>> fetchTopicDynamic(DynamicListParams params) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .fetchTopicDynamic(params.getPage(),
                        params.getPageSize(),
                        params.getTopicId(),
                        params.getTopicTitle(),
                        params.getIsHot(),
                        params.getType(),
                        params.getUserId())
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<DynamicResult> getDynamicById(int id) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .getDynamicById(id)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<CommentResult>> fetchComment(int dynamicId, int startId, int pageSize) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .fetchComment(dynamicId, startId, pageSize)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<CommentResult>> fetchReply(int commentId, int startId, int pageSize) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .fetchCommentByComment(commentId, startId, pageSize)
                .compose(RxUtils.handleResult());
    }


    @Override
    public Observable<Integer> delDynamic(int dynamicId) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .deleteDynamic(dynamicId)
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<Integer> comment(CommentParams params) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .comment(getRequestBody(JSON.toJSONString(params)))
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<Integer> commentReply(CommentReplyParams params) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .commentReply(getRequestBody(JSON.toJSONString(params)))
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<Integer> delComment(int type, int id) {
        if (type == 1) {//主评论
            return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                    .delComment(id)
                    .compose(RxUtils.handleCode());
        }
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .delCommentReply(id)
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<Integer> attention(int id) {
        Map<String, Object> params = new HashMap<>();
        params.put("beConcernId", id);
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .attention(getRequestBody(JSON.toJSONString(params)))
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<Integer> cancelAttention(int id) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .cancelAttention(id)
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<Integer> praise(int id) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .praise(id)
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<Integer> cancelPraise(int id) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .cancelPraise(id)
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<Integer> report(int dynamicId, int reportTypeId) {
        Map<String, Object> params = new HashMap<>();
        params.put("dynamicId", dynamicId);
        params.put("reportTypeId", reportTypeId);
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .report(getRequestBody(JSON.toJSONString(params)))
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<List<DynamicReportTypeResult>> fetchReportType() {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .fetchDynamicReportType()
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<Integer> shield(int uId, boolean block, boolean user) {
        Map<String, Object> params = new HashMap<>();
        params.put("resourceId", uId);
        params.put("statusFlag", block ? 1 : 0);
        params.put("type", user ? 2 : 1);
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .shield(getRequestBody(JSON.toJSONString(params)))
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<Integer> publishDynamic(DynamicPubParams params) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .publishDynamic(getRequestBody(JSON.toJSONString(params)))
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<String> getQiNiuToken() {
        return mRepositoryManager.obtainRetrofitService(CommonService.class)
                .getQiNiuToken()
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<TopicResult>> fetchTopic(String keyword) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .fetchTopic(1, 25, keyword, null)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<Integer> addTopic(String topicTitle) {
        Map<String, Object> params = new HashMap<>();
        params.put("topicTitle", topicTitle);
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .addTopic(getRequestBody(GsonUtils.toJson(params)))
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<TopicResult> getTopicById(int id) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .getTopicById(id)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<String> joinGroup(String groupId) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .joinChatGroup(groupId)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<FansResult>> fetchFansResult(int userId, int startId, int pageSize) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .fetchFansResult(userId, startId, pageSize)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<FansResult>> fetchAttentionResult(int userId, int startId, int pageSize) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .fetchAttentionResult(userId, startId, pageSize)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<AboutGoodsResult>> fetchAboutGoodsResult(int page, int pageSize) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .fetchAboutGoodsResult(page, pageSize)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<AboutGoodsResult>> fetchAboutCommentResult(int page, int pageSize) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .fetchAboutCommentResult(page, pageSize)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<AboutGoodsResult>> fetchAboutFansResult(int page, int pageSize) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .fetchAboutFansResult(page, pageSize)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<ShieldResult>> fetchShieldList(int page, int pageSize) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .fetchShieldList(page, pageSize, 2)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<Integer> updateUserInfo(UserInfoParams params) {
        return mRepositoryManager.obtainRetrofitService(AccountService.class)
                .updateUserInfo(getRequestBody(JSON.toJSONString(params)))
                .compose(RxUtils.handleCode());
    }

}
